<template>
<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
        <el-form-item label="供应商名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="供应商名称"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
            <el-input v-model="dataForm.addr" placeholder="地址"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman">
            <el-input v-model="dataForm.linkman" placeholder="联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="linkphone">
            <el-input v-model="dataForm.linkphone" placeholder="联系电话"></el-input>
        </el-form-item>
        <el-form-item label="开户行" prop="bank">
            <el-input v-model="dataForm.bank" placeholder="开户行"></el-input>
        </el-form-item>
        <el-form-item label="开户行账号" prop="bankNo">
            <el-input v-model="dataForm.bankNo" placeholder="开户行账号"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="info">
            <el-input type="textarea" autosize v-model="dataForm.info" placeholder="描述"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="note">
            <el-input v-model="dataForm.note" placeholder="备注"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
</el-dialog>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            dataForm: {
                id: 0,
                name: '',
                addr: '',
                linkman: '',
                linkphone: '',
                bank: '',
                bankNo: '',
                info: '',
                note: ''
            },
            dataRule: {
                name: [{
                    required: true,
                    message: '供应商名称不能为空',
                    trigger: 'blur'
                }],
                addr: [{
                    required: true,
                    message: '地址不能为空',
                    trigger: 'blur'
                }],
                linkman: [{
                    required: true,
                    message: '联系人不能为空',
                    trigger: 'blur'
                }],
                linkphone: [{
                    required: true,
                    message: '联系电话不能为空',
                    trigger: 'blur'
                }],
                bank: [{
                    required: true,
                    message: '开户行不能为空',
                    trigger: 'blur'
                }],
                bankNo: [{
                    required: true,
                    message: '开户行账号不能为空',
                    trigger: 'blur'
                }]
            }
        }
    },
    methods: {
        init(id) {
            this.dataForm.id = id || 0
            this.visible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                if (this.dataForm.id) {
                    this.$http({
                        url: this.$http.adornUrl(`/generator/tsupplier/info/${this.dataForm.id}`),
                        method: 'get',
                        params: this.$http.adornParams()
                    }).then(({
                        data
                    }) => {
                        if (data && data.code === 0) {
                            this.dataForm.name = data.tSupplier.name
                            this.dataForm.addr = data.tSupplier.addr
                            this.dataForm.linkman = data.tSupplier.linkman
                            this.dataForm.linkphone = data.tSupplier.linkphone
                            this.dataForm.bank = data.tSupplier.bank
                            this.dataForm.bankNo = data.tSupplier.bankNo
                            this.dataForm.info = data.tSupplier.info
                            this.dataForm.note = data.tSupplier.note
                        }
                    })
                }
            })
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    this.$http({
                        url: this.$http.adornUrl(`/generator/tsupplier/${!this.dataForm.id ? 'save' : 'update'}`),
                        method: 'post',
                        data: this.$http.adornData({
                            'id': this.dataForm.id || undefined,
                            'name': this.dataForm.name,
                            'addr': this.dataForm.addr,
                            'linkman': this.dataForm.linkman,
                            'linkphone': this.dataForm.linkphone,
                            'bank': this.dataForm.bank,
                            'bankNo': this.dataForm.bankNo,
                            'info': this.dataForm.info,
                            'note': this.dataForm.note
                        })
                    }).then(({
                        data
                    }) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        } else {
                            this.$message.error(data.msg)
                        }
                    })
                }
            })
        }
    }
}
</script>
